package bank;

import java.util.ArrayList;
import java.util.List;
import utils.Analyzer;
import utils.Stats;

public class ActiveAccountsAnalyzer implements Analyzer
{

	//Attribute that references the bank; needed for callback to get account balances for further analysis
	public Bank theBank = null;
	
	//initializes attribute
	public ActiveAccountsAnalyzer(Bank b)
	{
		this.theBank = b;
	}
		
	/**
	@return an ArrayList of the active account's balances 
	*/
	public List<Double> requestData()
	{
		List<Double> activeAccountsBalances = new ArrayList<Double>();
		for (BankAccount account : this.theBank.getList())
		{
			if ( (account.isOpen())||(account.isSuspended()) )
			{
				activeAccountsBalances.add(account.getBalance());
			}
		}
		return activeAccountsBalances;
	}
	
	/**
	Returns string that gives statistics of active accounts
	@return string giving the statistics of the active accounts
	*/
	public String analyze()
	{
		String returnString = "";
		for (BankAccount account : this.theBank.getList())
		{
			returnString = returnString + account.transactionsString();
		}
		returnString = returnString + "Analysis of active accounts\n   Active accounts = ";
		returnString = returnString + Stats.count(this) + "\n   Total balance = " + (int) Stats.sum(this) + "\n   ";
		returnString = returnString + "Average balance = " + (int) Stats.average(this) + " +/- " + (int) Stats.stdDeviation(this);
		returnString = returnString + "\n   Minimum balance = " + (int) Stats.min(this) + "\n   Maximum balance = " + (int) Stats.max(this);
		returnString = returnString + "\n\n\n";
		return returnString;
	}
}